package com.ruoyi.order.utils;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public  class zipUtils {



    public static void downloadZip(HttpServletResponse response, List<String> monthList,long storyId,String year) throws IOException {
        response.setContentType("application/octet-stream");
        int end = Integer.parseInt(year);
        int start=end-1;
        String file=start+"-"+end+".zip";
        response.setHeader("Content-Disposition", "attachment;fileName=" +
                new String(file.getBytes("gbk"), "iso8859-1"));
        ServletOutputStream os = response.getOutputStream();
        ZipOutputStream zos = new ZipOutputStream(os);

        byte[] buf = new byte[1024 * 2];
        for (String i : monthList) {

            zos.putNextEntry(new ZipEntry("订单\\" + i + "月订单"+".xlsx"));
            FileInputStream fis = new FileInputStream("D:\\JavaTest\\"+storyId+"\\" + i + ".xlsx");
            //使用字节缓冲输入流
            BufferedInputStream bis = new BufferedInputStream(fis);
            int len;
            while ((len = bis.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            zos.closeEntry();
            bis.close();
        }
        // 空文件夹的处理, 最后不加 / 会将c当成文件处理
//        zos.putNextEntry(new ZipEntry("a/d" + "/"));
        // 没有文件，不需要文件的copy
        zos.closeEntry();
        //必须要执行 zos.finish(); close()时内部会调用finish()
        zos.close();
    }


}
